<?php

namespace App\Http\Controllers\Mp;

use App\Http\Controllers\Controller;
use App\Model\Device;
use App\Model\Ttlock;
use App\Model\TtlockData;

use App\User;
use App\Utils\Result;

use Illuminate\Http\Request;

class TtlockController extends Controller
{
    /**新增用户
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function saveUser(Request $request){
        $type = $request->input('type');
        $username = $request->input('username');
        $user = $this->getUser();
        $password = $request->input('password');
        $password_hash = $request->input('password_hash');
        $check = Ttlock::query()->where('username',$username)->first();
        if($check){
            return Result::error(1001,'账户已存在');
        }
        if(!$username || !$password){
            return Result::error(1001,'账户信息不完整');
        }
        $ttlock = new Ttlock();
        $ttlock->type = $type;
        $ttlock->username = $username;
        $ttlock->password = $password;
        $ttlock->password_hash = $password_hash;
        $ttlock->user_id = $user->id;
        $ttlock->save();
        return Result::success();
    }

    /**修改密码
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function changePassword(Request $request){
        $username = $request->input('username');
        $password = $request->input('password');
        $password_hash = $request->input('password_hash');
        $lockUser = Ttlock::query()->where('username',$username)->first();
        if(!$lockUser){
            return Result::error(1001,'账户不存在');
        }
        $lockUser->password_hash = $password_hash;
        $lockUser->password = $password;
        $lockUser->save();
        return Result::success();
    }

    /**删除
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function deleteUser(Request $request){
        $username = $request->input('username');
        $lockUser = Ttlock::query()->where('username',$username)->first();
        if(!$lockUser){
            return Result::error(1001,'账户不存在');
        }
        $lockUser->delete();
        return Result::success();
    }

    public function userDetail(Request $request){
        $user = $this->getUser();
        $data = Ttlock::query()->where('user_id',$user->id)->get(['id','username','password_hash','type']);
        return Result::success($data);
    }

    public function updatePassword(Request $request){
        $lock_id = $request->input('lock_id');
        $mobile = $request->input('mobile');
        $password = $request->input('password');
        if($mobile){
            TtlockData::query()->where('lock_id',$lock_id)
                ->where('mobile',$mobile)
                ->update(['password'=>$password]);
        }else{
            TtlockData::query()->where('lock_id',$lock_id)
            ->update(['password'=>$password]);
        }
        return Result::success();
    }

    public function bind(Request $request){
        $lockId = $request->input('lockId');
        $user = $this->getUser();
        $keyId = $request->input('keyId');
        $house_id = $request->input('house_id');
        if(!$house_id){
            return Result::error(1001,'房源id不能为空');
        }
        $checkCount = Device::query()->where('house_id',$house_id)->count();
        if($checkCount <=  0){
            return Result::error(1001,'请先给房源添加电表');
        }

        $type =$request->input('type');
        if($type == 1){
            $user = $this->getUser();
            $mobile = $user->mobile;
        }else{
            $mobile = $request->input('mobile');
        }
        $check = TtlockData::query()->where('lock_id',$lockId)
            ->where('type',$type)
            ->where('mobile',$mobile)->first();
        if($check){
            return Result::error(1001,'该锁已绑定该用户');
        }
        $lock = new TtlockData();
        $lock->house_id = $house_id;
        $lock->type = $type;
        $lock->lock_id = $lockId;
        $lock->key_id = $keyId;
        $lock->mobile = $mobile;
        $lock->save();
        return Result::success($lock);
    }


    public function lockList(Request $request){
        $house_id = $request->input('house_id');
        if(!$house_id){
            $user = $this->getUser();
            $lock = TtlockData::query()
                ->where('mobile',$user->mobile)
                ->where('type',1)
                ->get();
        }else{
            $lock = TtlockData::query()
                ->where('house_id',$house_id)
                ->where('type',1)
                ->get();
        }
        return Result::success($lock);
    }

    public function keyList(Request $request){
        $lock_id = $request->input('lock_id');
        $type =$request->input('type',1);
        $list = TtlockData::query()->where('lock_id',$lock_id)
            ->where('type',$type)
            ->get();
        return Result::success($list);
    }

    /**冻结解冻
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function freeze(Request $request){
        $mobile = $request->input('mobile');
        $status = $request->input('status');
        $lock_id = $request->input('lock_id');
        if(!$mobile){
            TtlockData::query()->where('lock_id',$lock_id)
                ->where('type',2)
                ->update(['status'=>$status]);
        }else{
            TtlockData::query()->where('mobile',$mobile)
                ->where('lock_id',$lock_id)
                ->where('type',2)
                ->update(['status'=>$status]);
        }
        return Result::success();
    }


    public function unbind(Request $request){
        $lockId = $request->input('lockId');
        $house_id = $request->input('house_id');
        if(!$lockId){
            return Result::error(1001,'缺少参数通通锁id');
        }
        if(!$house_id){
            return Result::error(1001,'缺少参数房源id');
        }
        TtlockData::query()->where('lock_id',$lockId)
            ->where('house_id',$house_id)
            ->delete();
        return Result::success();

    }
}
